from django.http import JsonResponse, HttpResponse
from django.shortcuts import render, redirect
from django.views import View

from iClouds.apiheader import has_chocode
from iClouds.apiSerializers import *
from iClouds.wechatcfg import wxChatRequest  # ,wxConf
from qiniu import Auth

import time, json


class index(View):
    def get(self, request):
        print("Index: GET 方法")
        return HttpResponse("GET 方法")

    def post(self, request):
        print("Index: Post 方法")
        return HttpResponse("POST 方法")


class login(View):
    def get(self, request):
        # wxSDK = wxChatRequest()
        # token = wxSDK.hasToken()
        # print("\ntoken:", token)
        # code = has_chocode(6)
        # QrCode = wxSDK.hasQrCode(token.get("access_token"), code)
        # QrCode["code"] = code
        # print("\nQrCode:",QrCode)
        # return render(request, "file/login.html", QrCode)
        SDK = wxChatRequest()
        state = "resUpload"
        rect = "http%3a%2f%2fppt.3vppt.com%2fmymedia%2findex"
        QrCode = {
            "url": f"https://open.weixin.qq.com/connect/oauth2/authorize?appid={SDK.CONF.chat_appid}&redirect_uri={rect}&response_type=code&scope=snsapi_userinfo&state={state}#wechat_redirect",
            "index": "http://lease.3vppt.com:8000/file/login/",
        }
        return render(request, "file/login.html", QrCode)

    def post(self, request):
        return HttpResponse("POST 方法")


class upload(View):
    def get(self, request):
        return render(request, "file/upload.html")

    def post(self, request):
        return HttpResponse("POST 方法")


class fileList(View):
    def get(self, request):
        # r = PaySpotList.objects.all()  # filter(status=1)
        # dr = ModelPaySpotListSerializer(r, many=True).data

        return render(request, "file/fileList.html", {"data": ""})

    def post(self, request):
        return HttpResponse("POST 方法")


class uptoken(View):
    """七牛token"""

    def get(self, request):
        access_key = "EjQy0QGvHw7DWKwL5ZD9DrhcZWWkXv7P9eYZ8VKz"
        secret_key = "NoFBsyVXMtH1Eq63F5O9jg1KjjqlZ1nT7xfKSJXb"
        q = Auth(access_key, secret_key)
        # 要上传的空间
        bucket_name = "3vppt"
        # 上传后保存的文件名
        key = None
        # 生成上传 Token，可以指定过期时间等
        # 上传策略示例
        # https://developer.qiniu.com/kodo/manual/1206/put-policy
        # https://developer.qiniu.com/kodo/1206/put-policy
        policy = {
            # 'callbackUrl':'https://requestb.in/1c7q2d31',
            # 'callbackBody':'filename=$(fname)&filesize=$(fsize)'
            # 'persistentOps':'ppt_media',
            "forceSaveKey": True,
            "saveKey": "ppt_media/$(etag)&$(fname)",
            "returnUrl": None,
            "returnBody": json.dumps(
                {"key": "$(key)", "hash": "$(etag)", "fname": "$(fname)"}
            ),
        }
        # 3600为token过期时间，秒为单位。3600等于一小时
        # ppt_media
        # https://oss.3vppt.com/ppt_media/Fh6LNpKJu3uZpFYnJv72AmJTPUWI%26map.png
        token = q.upload_token(bucket_name, key, 3600, policy)
        return JsonResponse({"uptoken": token}, safe=True)

    def post(self, request):
        return HttpResponse("POST 方法")


class haskey(View):
    """文件名"""

    def get(self, request):
        key = f"{int(time.time())}{has_chocode(3)}"
        return HttpResponse(key)

    def post(self, request):
        return HttpResponse("POST 方法")
